
require 'fileutils'

if ARGV.length == 0
  puts "Usage: CreateProject.rb ProjectName"
  exit 1
end


def create_template_offspring(template_file, dest_file, mapping)
  FileUtils.copy(template_file, dest_file)
  File.open(template_file, 'r') do |source|
    File.open(dest_file, 'w') do |out|
      while (line = source.gets())
        mapping.each do |key, value|
          out << line.gsub(key, value)
        end
      end
    end
  end
end


$gVSVersions = [
  {
    :version => "2005",    
    :project_extension => "vcproj",
    :solution_extension => "sln",
    :postfix => "vs80"
  },
  {
    :version => "2008",
    :project_extension => "vcproj",
    :solution_extension => "sln",
    :postfix => "vs90"
  },
  {
    :version => "2010",
    :project_extension => "vcxproj",
    :solution_extension => "sln",
    :postfix => "vs100"
  }
]


def create_solution_file(projectName)
  $gVSVersions.each do |vs|
    source_file = "Sample_#{vs[:postfix]}.#{vs[:solution_extension]}"
    dest_file = "#{projectName}_#{vs[:postfix]}.#{vs[:solution_extension]}"
    puts "Creating #{dest_file}"
    create_template_offspring(source_file, dest_file, { "Sample" => projectName } )
  end
end

def create_solution_directory(projectName)
  system("svn export Sample #{projectName}")
  $gVSVersions.each do |vs|
    src_file = "#{projectName}/Sample_#{vs[:postfix]}.#{vs[:project_extension]}"
    dest_file = "#{projectName}/#{projectName}_#{vs[:postfix]}.#{vs[:project_extension]}"
    puts "Creating #{dest_file}"    
    create_template_offspring(src_file, dest_file, { "Sample" => projectName } )
    FileUtils.rm("#{projectName}/Sample_#{vs[:postfix]}.#{vs[:project_extension]}")
  end
end

projectName = ARGV[0]

create_solution_file(projectName)

create_solution_directory(projectName)

